//
//  HILoginViewController.m
//  HundredsICT
//
//  Created by chen wei on 14-2-20.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "HILoginViewController.h"
#import "RequestServer.h"
#import "BSMenuController.h"
#import "MSCreateDatabaseVC.h"

@interface HILoginViewController ()<RequestServerDelegate,UITextFieldDelegate>
{
    float hight;
}
@property (nonatomic, strong)UITextField *nameField;
@property (nonatomic, strong)UITextField *presswordField;
@property (nonatomic, strong)UIButton *loginButton;
@property (nonatomic, strong)UIImageView *imgv;
@property (nonatomic, strong)UIImageView *imgv1;
@end

@implementation HILoginViewController
@synthesize nameField,presswordField;

@synthesize loginButton;
@synthesize imgv,imgv1;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"登录";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    
    UIImageView * bg  = nil;
    if (isIOS7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"00b5ed"];
        bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
        hight = 40.0;
    }else{
        self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"00b5ed"];
        bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        hight = 0;
    }
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    
    bg.userInteractionEnabled = YES;
    bg.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    [self.view addSubview:bg];

   
    UIImageView *kuang1 = [[UIImageView alloc] initWithFrame:CGRectMake((320-143)/2, 30+hight, 143, 40)];
    kuang1.image = [UIImage imageNamed:@"logo.png"];
    [bg addSubview:kuang1];
    
    UIImageView *kuang = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90+hight, 300, 41.4)];
    kuang.image = [UIImage imageNamed:@"用户名.png"];
    [bg addSubview:kuang];
    [kuang release];
    
    UIImageView *kuang2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 132.4+hight, 300, 41.4)];
    kuang2.image = [UIImage imageNamed:@"密码.png"];
    [bg addSubview:kuang2];
    [kuang2 release];
    
    nameField = [[UITextField alloc]initWithFrame:CGRectMake(40, 90+hight, 260, 41.4)];
    [nameField setBorderStyle:UITextBorderStyleNone];
    nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    nameField.backgroundColor = [UIColor clearColor];
    nameField.delegate = self;
    nameField.tag = 01;
    [bg addSubview:nameField];
    
    presswordField = [[UITextField alloc]initWithFrame:CGRectMake(40, 132.4+hight, 260, 41.4)];
    [presswordField setBorderStyle:UITextBorderStyleNone];
    presswordField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    presswordField.delegate = self;
    presswordField.secureTextEntry = YES;
    presswordField.backgroundColor = [UIColor clearColor];
    presswordField.tag = 02;
    [bg addSubview:presswordField];
    
    
    loginButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 190+hight, 300,41.4)];
    [loginButton setImage:[UIImage imageNamed:@"登录.png"] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:loginButton];
    
}



- (void)login
{
//    BSMenuController *menu = [[BSMenuController alloc]init];
//    [self.navigationController pushViewController:menu animated:YES];
//    return;
    
    [nameField resignFirstResponder];
    [presswordField resignFirstResponder];
    
    
    NSString *name =nameField.text;
    NSString *password =presswordField.text;
    if (!name || !password || [name isEqualToString:@""] || [password isEqualToString:@""]) {
        ShowMessage(@"请输入用户名或密码");
        return;
    }
    
    NSMutableDictionary *defaults1 = [NSMutableDictionary dictionary];
    
    [defaults1 setObject:name forKey:@"loginName"];
    [defaults1 setObject:[self md5:password]  forKey:@"passWord"];
    [defaults1 setObject:@"1" forKey:@"appVersion"];
    [defaults1 setObject:@"mac" forKey:@"deviceId"];
    
    [[RequestServer instance] doActionAsync:0 withAction:@"LoginAction$login" withParameters:defaults1 withDelegate:self];
}


-(NSString *)md5:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[32];
    CC_MD5( cStr, strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [nameField resignFirstResponder];
    [presswordField resignFirstResponder];
}

- (void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data
{
    NSLog(@"%d",[code intValue]);
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([code intValue] == 0) {
        if ([data objectForKey:@"userid"]) {
            [defaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"userid"]] forKey:@"userId"];
        }
        if ([data objectForKey:@"username"]) {
            [defaults setObject:[data objectForKey:@"username"] forKey:@"username"];
        }
        if ([data objectForKey:@"loginname"]) {
            [defaults setObject:[data objectForKey:@"loginname"] forKey:@"loginname"];
        }
        if ([data objectForKey:@"mobile"]) {
            [defaults setObject:[data objectForKey:@"mobile"] forKey:@"mobile"];
        }
        if ([data objectForKey:@"phone"]) {
            [defaults setObject:[data objectForKey:@"phone"] forKey:@"phone"];
        }
        if ([data objectForKey:@"email"]) {
            [defaults setObject:[data objectForKey:@"email"] forKey:@"email"];
        }
       
        BSMenuController *menu = [[BSMenuController alloc]init];
        [self.navigationController pushViewController:menu animated:YES];
        
        [[MSCreateDatabaseVC instance]doCreateTable];
        

    }else if ([code intValue]== 1) {
        ShowMessage(message);
    }else{
        ShowMessage(@"登录 ---请求失败");
    }
    self.view.userInteractionEnabled = YES;
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField.tag == 01) {
        imgv.frame = CGRectMake(10, 80, 300, 45);
        imgv1.frame = CGRectMake(10, 140, 300, 45);
    }else if(textField.tag == 02){
        imgv.frame = CGRectMake(10, 80, 300, 45);
        imgv1.frame = CGRectMake(10, 140, 300, 45);
    }
    
    return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [nameField resignFirstResponder];
    [presswordField resignFirstResponder];
    
    return YES;
}

- (void)dealloc
{
    [presswordField release];
    [nameField release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
